/*
 * CS_WORLD_BOSS_RANK.cpp
 *
 *  Created on: 2014. 1. 13.
 *      Author: jjaehuny
 */


#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Space.h"
#include "../../NResource/Static.h"

#include <sstream>

namespace ProjectZ { namespace NCommand {

static void SendAck_CS_WORLD_BOSS_RANK_REWARD_INFO(User * pUser, int16_t ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_WORLD_BOSS_RANK_REWARD_INFO);
	pUser->Send(pAck);
}

void Command::CS_WORLD_BOSS_RANK_REWARD_INFO(User * pUser, ::Network::Packet * pPacket)
{
	/*
	CS_WORLD_BOSS_RANK					0x4015
	{
	}
	SC_WORLD_BOSS_RANK					0x4016
	{
		s16 sAck;
		s32 s32my_rank;				/// -1: 랭킹없음
		u32 u32my_total_point;
		u8 u8is_get_reward;         //0: 안받음, 1: 받음, 2: 보상 준비중

		u8 u8count;
		{
			u8 u8Min;				//보상 최소 조건 ex) 랭킹 4등에서
			u8 u8Max;				//보상 최대 조건 ex) 랭킹 5등까지

			/// 아이템
			u8 u8reward_type; 		//0: 아이템, 1: 골드, 2, 없음 (보상 내용이 변경될일 없는지 협의 필요!)
			u32 u32reward_value;	//보상 갯수
			u8 u8class_type;
			u8 u8sheet_type;		// 골드일땐 잡화에 있는 골드 TID
			u16	u16tid;				// 테이블 아이디

			/// 이름표
			u8 u8reward_type2; 		//0: 아이템, 1: 골드, 2, 없음 (보상 내용이 변경될일 없는지 협의 필요!)
			u32 u32reward_value2;	//보상 갯수
			u8 u8class_type2;
			u8 u8sheet_type2;		// 골드일땐 잡화에 있는 골드 TID
			u16	u16tid2;			// 테이블 아이디
		}
	}
	*/

	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("%s NULL == pWorld", __FUNCTION__);
		SendAck_CS_WORLD_BOSS_RANK_REWARD_INFO(pUser, ACK_NO_SPACE);
		return;
	}

	if (pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("%s pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD", __FUNCTION__);
		SendAck_CS_WORLD_BOSS_RANK_REWARD_INFO(pUser, ACK_NO_SPACE);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::__NONE__", __FUNCTION__);
		SendAck_CS_WORLD_BOSS_RANK_REWARD_INFO(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::EventTimeResource * pEventTimeResource = NResource::Static::Instance().GetEventTime();
	if (NULL == pEventTimeResource)
	{
		DEBUGLOG("%s NULL == pEventTimeResource", __FUNCTION__);
		SendAck_CS_WORLD_BOSS_RANK_REWARD_INFO(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NResource::WorldBoss * pWorldBossResource = NResource::Static::Instance().GetWorldBossResource();
	if (NULL == pWorldBossResource)
	{
		DEBUGLOG("%s NULL == pWorldBossResource", __FUNCTION__);
		SendAck_CS_WORLD_BOSS_RANK_REWARD_INFO(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	int rank = -1;
	{
		::NoSQL::Command::Redis clsRedis;
		clsRedis._eServer = ::NoSQL::Command::BaseInfo::REDIS;
		clsRedis._eAction = ::NoSQL::Command::Redis::ZREVRANK;
		clsRedis._key = "RANK";
		std::stringstream stream;
		stream << pUser->GetUserSeq();
		clsRedis._value = stream.str();

		::NoSQL::Command::Auto clsAuto(::NoSQL::GETCOMMANDPOOL());
		clsAuto->SetRedis(clsRedis);

		::NoSQL::SESSION()->SyncExecute(static_cast<int>(RedisServerType::WORLDBOSS_MASTER), clsAuto);

		if (0 == clsAuto->GetResult())
		{
			DEBUGLOG("ZREVRANK RANK : %d", clsAuto->GetRedis()._outRank);
			rank = clsAuto->GetRedis()._outRank;
		}
		else
		{
			DEBUGLOG("ZRANK RESULT : %d", clsAuto->GetResult());
		}
	}

	double score = 0;
	{
		::NoSQL::Command::Redis clsRedis;
		clsRedis._eServer = ::NoSQL::Command::BaseInfo::REDIS;
		clsRedis._eAction = ::NoSQL::Command::Redis::ZSCORE;
		clsRedis._key = "RANK";
		std::stringstream stream;
		stream << pUser->GetUserSeq();
		clsRedis._value = stream.str();

		::NoSQL::Command::Auto clsAuto(::NoSQL::GETCOMMANDPOOL());
		clsAuto->SetRedis(clsRedis);

		::NoSQL::SESSION()->SyncExecute(static_cast<int>(RedisServerType::WORLDBOSS_MASTER), clsAuto);

		if (0 == clsAuto->GetResult())
		{
			DEBUGLOG("ZREVRANK RANK : %d", clsAuto->GetRedis()._outRank);
			score = clsAuto->GetRedis()._outScore;
		}
		else
		{

		}
	}

	uint8_t reward = 1;
	bool bReward = true;

	::NoSQL::Command::Redis clsRedis;
	clsRedis._eServer = ::NoSQL::Command::BaseInfo::REDIS;
	clsRedis._eAction = ::NoSQL::Command::Redis::EXISTS;
	std::stringstream stream;
	stream << pUser->GetUserSeq();
	clsRedis._key = stream.str();

	::NoSQL::Command::Auto clsAuto(::NoSQL::GETCOMMANDPOOL());
	clsAuto->SetRedis(clsRedis);

	::NoSQL::SESSION()->SyncExecute(static_cast<int>(RedisServerType::WORLDBOSS_MASTER), clsAuto);

	if (0 == clsAuto->GetResult())
	{
		DEBUGLOG("ZCARD count : %d", clsAuto->GetRedis()._count);
		bReward = clsAuto->GetRedis()._isExist;
	}
	else
	{
		DEBUGLOG("ZCARD clsAuto->GetResult() : %d", clsAuto->GetResult());
	}


	if (true == pEventTimeResource->IsWorldBossReward(pUser) && false == bReward)
	{
		reward = 0;
	}
	else if(false == pEventTimeResource->IsWorldBossReward(pUser) && false == bReward)
	{
		reward = 2;
	}
	else {}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK);
	if (-1 != rank)
		rank += 1;
	* pAck << static_cast<int32_t>(rank);
	//* pAck << static_cast<uint64_t>(pUser->GetUserInfo().worldboss_point);
	* pAck << static_cast<uint64_t>(score);
//	if (true == bReward)
//		* pAck << static_cast<uint8_t>(1);
//	else
//		* pAck << static_cast<uint8_t>(0);
	* pAck << static_cast<uint8_t>(reward);


	NResource::WorldBossReward::DATAVECTOR & dataVector = NResource::Static::Instance().GetWorldBossRewardResource()->GetDataVector();

	* pAck << static_cast<uint8_t>(dataVector.size());

	std::for_each(dataVector.begin(), dataVector.end(), [&](NResource::WorldBossReward::Data & data) {
		* pAck << static_cast<uint32_t>(data.rank_min);
		* pAck << static_cast<uint32_t>(data.rank_max);

		* pAck << static_cast<uint8_t>(data.reward1_type);
		* pAck << static_cast<uint32_t>(data.reward1_value);
		* pAck << static_cast<uint8_t>(data.reward1_class_type);
		* pAck << static_cast<uint8_t>(data.reward1_sheet_type);
		* pAck << static_cast<uint16_t>(data.reward1_tid);

		* pAck << static_cast<uint8_t>(data.reward2_type);
		* pAck << static_cast<uint32_t>(data.reward2_value);
		* pAck << static_cast<uint8_t>(data.reward2_class_type);
		* pAck << static_cast<uint8_t>(data.reward2_sheet_type);
		* pAck << static_cast<uint16_t>(data.reward2_tid);

	} );

	pAck->MakePacket(CMD_SC_WORLD_BOSS_RANK_REWARD_INFO);
	pUser->Send(pAck);

}

} /*NCommand*/ } /*ProjectZ*/


